package com.dhu.demo.controller;

import com.dhu.demo.entity.Sc;
import com.dhu.demo.entity.ScKey;
import com.dhu.demo.entity.InsertSc;
import com.dhu.demo.service.ClassTableService;
import com.dhu.demo.service.ScService;
import org.apache.ibatis.annotations.Param;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/Sc")
@RestController
@CrossOrigin("*")
public class ScController {
    @Autowired
    private ScService scService;
    @Autowired
    private ClassTableService classTableService;

    @RequestMapping("/getAllScore")
    public List<Sc> getallscore(@RequestBody Sc sc){
        int studentnumber=sc.getStudentnumber();
        String xueqi=sc.getXueqi();
        return scService.getallscore(studentnumber,xueqi);
    }

    @RequestMapping("/findAllSc")
    public List<Sc> getSc(){
        return scService.findAll();
    }

    @RequestMapping("/insertSc")
    private int insertScByClassteacherAndClassname(@RequestBody InsertSc insertSc){
        Integer studentnumber=insertSc.getStudentnumber();
        String classname=insertSc.getClassname();
        Integer score=insertSc.getScore();
        String xueqi="2018下";
        String grade=classTableService.calculatingLevel(classname,score);
        Sc record=new Sc(studentnumber,classname,score,grade,xueqi);
        return scService.insert(record);
    }

    @RequestMapping("changeStudentScore")
    private int updateScByClassteacherAndClassname(@RequestBody InsertSc insertSc){
        Integer studentnumber=insertSc.getStudentnumber();
        String classname=insertSc.getClassname();
        Integer score=insertSc.getScore();
        String xueqi=insertSc.getXueqi();
        //String grade=classTableService.calculatingLevel(classname,classteacher,score);
        String grade=classTableService.calculatingLevel(classname,score);
        Sc record=new Sc(studentnumber,classname,score,grade,xueqi);

        return scService.updateByPrimaryKey(record);
    }


    @RequestMapping("/updateSc")
    private int updateSc(@RequestBody Sc sc){
        int studentnumber=sc.getStudentnumber();
        String classname=sc.getClassname();
        int score=sc.getScore();
        String grade=sc.getGrade();
        String xueqi=sc.getXueqi();
        Sc record=new Sc(studentnumber,classname,score,grade,xueqi);
        return scService.updateByPrimaryKey(record);
    }

    @RequestMapping("/deleteSc")
    private int deleteInfo(@RequestBody Sc sc){
        int studentnumber=sc.getStudentnumber();
        String classname=sc.getClassname();
        ScKey record=new ScKey(studentnumber,classname);
        return scService.deleteByPrimaryKey(record);
    }

}
